package com.mutual.help.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mutual.help.entity.Dianzan;
import com.mutual.help.service.DianzanService;
import com.mutual.help.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DianzanController {

    @Autowired
    private DianzanService dianzanService;

    @RequestMapping("wx/addDianzan")
    @ResponseBody
    public Object addDianzan(@RequestBody Dianzan dianzan){
        if (dianzanService.save(dianzan))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    @RequestMapping("wx/isDianzan")
    @ResponseBody
    public Object isDianzan(@RequestBody Dianzan dianzan){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (dianzan != null) {
            if (dianzan.getQuestionId()!=null) {
                queryWrapper.eq("question_id", dianzan.getQuestionId());
            }
            if (dianzan.getUserId() != null) {
                queryWrapper.eq("user_id", dianzan.getUserId());
            }
        }
        List<Dianzan> list = dianzanService.list(queryWrapper);
        if (list.size() > 0)
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    //取消点赞
    @RequestMapping("wx/cancelDianzan")
    @ResponseBody
    public Object cancelDianzan(@RequestBody Dianzan dianzan){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (dianzan != null) {
            if (dianzan.getQuestionId()!=null) {
                queryWrapper.eq("question_id", dianzan.getQuestionId());
            }
            if (dianzan.getUserId() != null) {
                queryWrapper.eq("user_id", dianzan.getUserId());
            }
        }
        if (dianzanService.remove(queryWrapper))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }
}
